import _ from "lodash";
import { GetDateDiff } from "@/utils/currentDate";
// 拼接多个参数的sql
export const renderSqlType = type => {
  if (Object.prototype.toString.call(type) == "[object String]") {
    return `max(meta_${type}) as ${type}`;
  }
  let strArr = [];
  type.forEach(token => {
    strArr.push(`max(meta_${token}) as ${token}`);
  });
  return strArr.join(",");
};
// 拼接sql
export const renderSql = params => {
  if (!params) {
    console.warn("请传出参数");
    return;
  }
  const {
    type, //点位  //['meter1RealTemp','meter2RealTemp']
    startTime, //开始时间
    endTime, //结束时间
    intervalTime, //时间间隔  //10s
    tenantToken, //租户
    assignmentToken, //设备分配
    deviceEventType //点位类型 //Measurement
  } = params;
  const typeStr = renderSqlType(type);
  return (
    "select first(type) as type, " +
    typeStr +
    " from td_" +
    tenantToken +
    ".`" +
    assignmentToken +
    "` where type=='" +
    deviceEventType +
    "' and time >= '" +
    startTime +
    "' and time <= '" +
    endTime +
    "' interval(" +
    intervalTime +
    ")"
  );
};

/**
时间间隔规则：（单位秒（s）,分钟（m））
时间段小于等于2天，时间间隔为10秒；
时间段大于2天并小于等于30天，时间间隔为1分钟。
时间段大于30天并小于360天，计算间隔时间值，该值取整数，公式为：最大天数*86400/20000 = 时间间隔（单位为秒）。
时间间隔不能大于360天
**/
// 设置时间间隔
export const setIntervalTime = searchForm => {
  // 获取两个时间的天数间隔
  const day = GetDateDiff(searchForm.startDate, searchForm.endDate, "day");
  console.log("相差天数 :>> ", day);
  if (day > 360) {
    return false;
  }
  if (day <= 2) {
    return "10s";
  }
  if (day > 2 && day <= 30) {
    return "1m";
  }
  if (day > 30) {
    // 间隔天数*86400/20000
    return `${Math.floor((day * 86400) / 20000)}s`;
  }
};

export const COLORS = [
  "#00ADD0",
  "#FFA12F",
  "#B62AFF",
  "#604BFF",
  "#6E35FF",
  "#002AFF",
  "#20C0F4",
  "#95F300",
  "#04FDB8",
  "#AF5AFF",
  "#73A0FA",
  "#73DEB3",
  "#FFB761",
  "#3EC1D3",
  "#ffcbcb",
  "#e5df88",
  "#d2e603",
  "#515070"
];
